package com.welson.db.cryptor.config;

import com.welson.db.cryptor.core.EntityCryptProcessorFactory;
import com.welson.db.cryptor.interceptor.CryptInterceptor;
import com.welson.db.cryptor.strategy.CryptStrategyFactory;
import com.welson.db.cryptor.util.Cryptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 数据库字段加解密自动配置类
 * @author welson
 */
@Configuration
@EnableConfigurationProperties(DbCryptorProperties.class)
@ConditionalOnProperty(prefix = "db.cryptor", name = "enabled", havingValue = "true", matchIfMissing = true)
public class DbCryptorAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public CryptStrategyFactory cryptStrategyFactory(DbCryptorProperties properties) {
        return new CryptStrategyFactory(properties);
    }

    @Bean
    @ConditionalOnMissingBean
    public Cryptor cryptor(DbCryptorProperties properties, CryptStrategyFactory strategyFactory) {
        return new Cryptor(properties, strategyFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public EntityCryptProcessorFactory entityCryptProcessorFactory(DbCryptorProperties properties) {
        return new EntityCryptProcessorFactory(properties);
    }

    @Bean
    @ConditionalOnMissingBean
    public CryptInterceptor cryptInterceptor(DbCryptorProperties properties, EntityCryptProcessorFactory processorFactory) {
        return new CryptInterceptor(properties, processorFactory);
    }
} 